package com.ruoyi.moon.service.impl;

import java.util.List;

import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.ruoyi.common.config.MySensitiveWordReplace;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.moon.domain.MoonReviews;
import com.ruoyi.moon.domain.vo.CompanyReviewVO;
import com.ruoyi.moon.mapper.MoonReviewsMapper;
import com.ruoyi.moon.service.IMoonReviewsService;
import com.ruoyi.system.domain.SysEventTracking;
import com.ruoyi.system.service.ISysEventTrackingService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.moon.mapper.MoonCompanyMapper;
import com.ruoyi.moon.domain.MoonCompany;
import com.ruoyi.moon.service.IMoonCompanyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公司管理Service业务层处理
 * 
 * @author admin
 * @date 2024-11-05
 */
@Service
public class MoonCompanyServiceImpl implements IMoonCompanyService 
{
    @Autowired
    private MoonCompanyMapper moonCompanyMapper;

    @Autowired
    private MoonReviewsMapper moonReviewsMapper;

    @Autowired
    private IMoonReviewsService moonReviewsService;

    @Autowired
    private ISysEventTrackingService eventTrackingService;
    /**
     * 查询公司管理
     * 
     * @param id 公司管理主键
     * @return 公司管理
     */
    @Override
    public MoonCompany selectMoonCompanyById(Long id)
    {
        MoonCompany company = moonCompanyMapper.selectMoonCompanyById(id);
        if(null != company){
            List<MoonReviews> moonReviews = moonReviewsMapper.selectMoonCompanyReviewsById(company.getId());
            company.setReviewsCount(moonReviews.size());
        }

        return company;
    }

    /**
     * 查询公司管理列表
     * 
     * @param moonCompany 公司管理
     * @return 公司管理
     */
    @Override
    public List<MoonCompany> selectMoonCompanyList(MoonCompany moonCompany)
    {
        List<MoonCompany> companyList = moonCompanyMapper.selectMoonCompanyList(moonCompany);
        for (MoonCompany companies : companyList){
            List<MoonReviews> reviews =  moonReviewsMapper.selectMoonCompanyReviewsById(companies.getId());
            companies.setMoonReviewsList(reviews);
            companies.setReviewsCount(reviews.size());
            SysEventTracking eventTracking = new SysEventTracking();
            eventTracking.setTables("moon_company");
            eventTrackingService.selectSysEventTrackingList(eventTracking);
        }
        return companyList;
    }

    /**
     * 新增公司管理
     * 
     * @param moonCompany 公司管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMoonCompany(MoonCompany moonCompany)
    {
        moonCompany.setCreateTime(DateUtils.getNowDate());
        return  moonCompanyMapper.insertMoonCompany(moonCompany);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertCompany(MoonCompany moonCompany)
    {
        if (StringUtils.isEmpty(moonCompany.getCompanName())){
            throw new IllegalArgumentException("公司名称不能为空");
        }else if (moonCompany.getCompanyRating().equals(0.0)){
            throw new IllegalArgumentException("公司评分不能为空");
        }
        moonCompany.setCreateTime(DateUtils.getNowDate());
        moonCompanyMapper.insertMoonCompany(moonCompany);

        MoonReviews moonReviews = new MoonReviews();
        moonReviews.setUserId(moonCompany.getUserId());
        moonReviews.setCompanyRating(moonCompany.getCompanyRating());
        moonReviews.setCompanyName(moonCompany.getCompanName());
        moonReviews.setCompanyId(moonCompany.getId());
        String replacedDescription  = SensitiveWordHelper.replace(moonCompany.getDetailedDescription(), new MySensitiveWordReplace());
        moonReviews.setDetailedDescription(replacedDescription);
        moonReviews.setCreateTime(DateUtils.getNowDate());
        moonReviews.setYouWork(moonCompany.getYouWork());
        int i = moonReviewsMapper.insertMoonReviews(moonReviews);
        //审核
        moonReviews.setAuditStatus("0");
        moonReviewsService.auditSubmit(moonReviews);
        return i;
    }

    /**
     * 修改公司管理
     * 
     * @param moonCompany 公司管理
     * @return 结果
     */
    @Override
    public int updateMoonCompany(MoonCompany moonCompany)
    {
        return moonCompanyMapper.updateMoonCompany(moonCompany);
    }

    /**
     * 批量删除公司管理
     * 
     * @param ids 需要删除的公司管理主键
     * @return 结果
     */
    @Override
    public int deleteMoonCompanyByIds(Long[] ids)
    {
        return moonCompanyMapper.deleteMoonCompanyByIds(ids);
    }

    /**
     * 删除公司管理信息
     * 
     * @param id 公司管理主键
     * @return 结果
     */
    @Override
    public int deleteMoonCompanyById(Long id)
    {
        return moonCompanyMapper.deleteMoonCompanyById(id);
    }

    @Override
    public List<MoonCompany> selectMoonCompanyName(String companName) {
        return moonCompanyMapper.selectMoonCompanyName(companName);
    }

    @Override
    public List<CompanyReviewVO> selectCompanyReviews(CompanyReviewVO companyReviewVO) {
        return moonCompanyMapper.selectCompanyReviews(companyReviewVO);
    }

    @Override
    public List<MoonCompany> selectMoonCompanyReviewsList(MoonCompany moonCompany) {


        List<MoonCompany> compa = moonCompanyMapper.selectMoonCompanyReviewsList(moonCompany);

        for (MoonCompany com : compa){
            List<MoonReviews> reviews =  moonReviewsMapper.selectReviewsById(com.getId());
            com.setMoonReviewsList(reviews);
            com.setReviewsCount(reviews.size());
            SysEventTracking eventTracking = new SysEventTracking();
            eventTracking.setTables("moon_company");
            eventTrackingService.selectSysEventTrackingList(eventTracking);
        }

        return compa;
    }


}
